package com.yanqu.road.server.cmd.chefduel.process2;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.List;

@Cmd(code = Protocol.S_CHEF_DUEL2_GET_CAN_JOIN_USER, desc = "获取一个开宴且自己可以坐的用户")
public class ChefDuel2GetCanJoinCookerCmd extends AbstractCommand {

	int activityId;
	long nowUserId;
	int otherActivityId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		ChefDuelProto.ChefDuel2CanJoinUserReqMsg reqMsg = ChefDuelProto.ChefDuel2CanJoinUserReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		nowUserId = reqMsg.getHomeUserId();
		if(reqMsg.hasOtherActivityId()){
			otherActivityId = reqMsg.getOtherActivityId();
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = ChefDuelActivityMgr.getOpenActivityInfo(activityId);
		if(activityInfo == null){
			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}

		ChefDuelProto.CrossChefDuel2CanJoinUserReqMsg.Builder reqMsg = ChefDuelProto.CrossChefDuel2CanJoinUserReqMsg.newBuilder();
		reqMsg.setActivityId(activityInfo.getActivityId());
		reqMsg.setHomeUserId(nowUserId);
		List<Integer> spoonIdList = ChefDuelActivityMgr.getChefDuel2SpoonIdList(activityId);
		for(Integer spoonId : spoonIdList){
			if(player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(spoonId + "=1"))){
				reqMsg.addSpoonId(spoonId);
			}
		}
		if(otherActivityId > 0 && otherActivityId != activityId){
			//请求赴宴范围的活动
			reqMsg.setActivityId(otherActivityId);
			ChefDuelMgr.getLogger().info("玩家{}，请求赴宴范围活动{}的可吃桌子", player.getUserId(), otherActivityId);
		}
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_CHEF_DUEL2_GET_CAN_JOIN_USER, reqMsg);
		GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			ChefDuelProto.ChefDuel2CanJoinUserRespMsg.Builder respMsg = ChefDuelProto.ChefDuel2CanJoinUserRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_CHEF_DUEL2_GET_CAN_JOIN_USER, respMsg);
		}
	}
}
